package com.hcy.userapi.manager;

import com.hcy.business.service.impl.UserServiceBzImpl;
import com.hcy.rabbitbasicmodel.dto.MyPage;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.signl.Add;
import com.hcy.rabbitbasicmodel.signl.Delete;
import com.hcy.rabbitbasicmodel.signl.Update;
import com.hcy.userdomin.domain.entity.Role;
import com.hcy.usermodel.dto.request.*;
import com.hcy.usermodel.dto.response.MenuResponseDTO;
import com.hcy.usermodel.dto.response.RoleResponseDTO;
import com.hcy.usermodel.dto.response.RoutesResponseDTO;
import com.hcy.usermodel.dto.response.UserAuthsResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/4/20 20:00
 */
@RestController
@Slf4j
@RequestMapping("/manager")
public class UserInfoController {

    @Autowired
    UserServiceBzImpl userServiceBz;


    /**
     * @Author huchenying
     * @Description 上传路由信息
     * @Date 2022/4/20 20:01
     **/
    @PostMapping("/uploadRoutes")
    public ResponseData<Object> uploadRoutes(@RequestBody UploadRoutesRequestDTO requestDTO) {
        log.info("uploadRoutes start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            userServiceBz.addRoutes(requestDTO);
            return ResponseData.success();
        } catch (Exception e) {
            log.error("路由表添加失败 e -> {}", ToStringBuilder.reflectionToString(e));
            return ResponseData.fail0("路由表添加失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取路由信息，获取5条，按时间倒序
     * @Date 2022/4/20 20:01
     **/
    @PostMapping("/getRoutesList")
    public ResponseData<List<RoutesResponseDTO>> getRoutesList() {
        log.info("uploadRoutes start");
        ResponseData<List<RoutesResponseDTO>> responseData = new ResponseData<>();
        try {
            List<RoutesResponseDTO> responseDTOList = userServiceBz.getRoutesList();
            responseData.setCode(Code.Success);
            responseData.setMsg("获取路由表成功，第一条为最新路由表");
            responseData.setData(responseDTOList);
            log.info("res -> {}", ToStringBuilder.reflectionToString(responseDTOList));
            return responseData;
        } catch (Exception e) {
            log.error("e -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("获取路由表失败");
            return responseData;
        }
    }

    /**
     * @Author huchenying
     * @Description 获取用户列表
     * @Date 2022/4/21 17:46
     **/
    @PostMapping("/getUserList")
    public ResponseData<MyPage<UserAuthsResponseDTO>> getUserList(@RequestBody UserAuthRequestDTO requestDTO) {
        log.info("getUserList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<MyPage<UserAuthsResponseDTO>> responseData = new ResponseData<>();
        try {
            MyPage<UserAuthsResponseDTO> responseDTOList = userServiceBz.getUserList(requestDTO);
            responseData.success(responseDTOList);
            log.info("result -> {}", ToStringBuilder.reflectionToString(responseDTOList));
            return responseData;
        } catch (Exception e) {
            log.error("获取用户列表失败！");
            return responseData.fail("获取用户列表失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取乘客用户列表
     * @Date 2022/4/21 17:46
     **/
    @PostMapping("/getCustomerList")
    public ResponseData<MyPage<UserAuthsResponseDTO>> getCustomerList(@RequestBody UserAuthRequestDTO requestDTO) {
        log.info("getCustomerList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<MyPage<UserAuthsResponseDTO>> responseData = new ResponseData<>();
        try {
            MyPage<UserAuthsResponseDTO> responseDTOList = userServiceBz.getCustomerList(requestDTO);
            responseData.success(responseDTOList);
            log.info("result -> {}", ToStringBuilder.reflectionToString(responseDTOList));
            return responseData;
        } catch (Exception e) {
            log.error("获取用户列表失败！");
            return responseData.fail("获取用户列表失败");
        }
    }


    /**
     * @Author huchenying
     * @Description 插入或更新系统用户
     * @Date 2022/4/21 18:01
     **/
    @PostMapping("/insertOrUpdate")
    public ResponseData<UserAuthsResponseDTO> insertOrUpdate(@RequestBody UserAuthRequestDTO requestDTO) {
        log.info("insertOrUpdate start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<UserAuthsResponseDTO> responseData = new ResponseData<>();
        try {
            UserAuthsResponseDTO responseDTO = userServiceBz.insertOrUpdateManager(requestDTO);
            return responseData.success(responseDTO);
        } catch (Exception e) {
            log.info("insertOrUpdate e -> {}", "");
            return responseData.fail("添加失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 删除一个用户
     * @Date 2022/4/21 18:45
     **/
    @PostMapping("/deleteUser")
    public ResponseData<Object> deleteUser(@Validated(Delete.class) @RequestBody UserAuthRequestDTO requestDTO) {
        log.info("delete start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            userServiceBz.deleteUser(requestDTO);
            return ResponseData.success();
        } catch (Exception e) {
            log.error("删除用户失败");
            return ResponseData.fail0("添加角色失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取所有角色以及角色的菜单列表
     * @Date 2022/4/21 18:01
     **/
    @PostMapping("/getRoleList")
    public ResponseData<List<RoleResponseDTO>> getRoleList(@RequestBody UserAuthRequestDTO requestDTO) {
        log.info("getRoleList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<List<RoleResponseDTO>> responseData = new ResponseData<>();
        try {
            List<RoleResponseDTO> responseDTO = userServiceBz.getRoleList(requestDTO);
            return responseData.success(responseDTO);
        } catch (Exception e) {
            log.info("getRoleList e -> {}", "");
            return responseData.fail("添加失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 添加一个角色
     * @Date 2022/4/21 18:40
     **/
    @PostMapping("/addRole")
    public ResponseData<RoleResponseDTO> addRole(@Validated(Add.class) @RequestBody RoleResponseDTO requestDTO) {
        log.info("addRole start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<RoleResponseDTO> responseData = new ResponseData<>();
        try {
            RoleResponseDTO responseDTO = userServiceBz.addRole(requestDTO);
            return responseData.success(responseDTO);
        } catch (Exception e) {
            log.error("添加角色失败");
            return responseData.fail("添加角色失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 删除一个角色
     * @Date 2022/4/21 18:40
     **/
    @PostMapping("/deleteRole")
    public ResponseData<Object> deleteRole(@RequestBody RoleResponseDTO requestDTO) {
        log.info("deleteRole start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            userServiceBz.deleteRole(requestDTO);
            return ResponseData.success();
        } catch (Exception e) {
            log.error("删除角色失败");
            return ResponseData.fail0("删除角色失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 更新用户角色信息
     * @Date 2022/4/21 18:54
     **/
    @PostMapping("/updateUserRole")
    public ResponseData<Object> updateUserRole(@RequestBody RoleUpdateRequestDTO requestDTO) {
        log.info("updateUserRole start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            userServiceBz.updateUserRole(requestDTO);
            return ResponseData.success();
        } catch (Exception e) {
            log.error("更新角色失败 e->{}", ToStringBuilder.reflectionToString(e));
            return ResponseData.fail0("更新角色失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 添加一条操作日志
     * @Date 2022/4/21 21:50
     **/
    @PostMapping("/addOptLog")
    public ResponseData<Object> addOptLog(@Validated @RequestBody AddLogRequestDTO requestDTO) {
        log.info("addOptLog start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            userServiceBz.addOptLog(requestDTO);
            return ResponseData.success();
        } catch (Exception e) {
            log.error("更新角色失败");
            return ResponseData.fail0("更新角色失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取菜单列表
     * @Date 2022/4/22 23:16
     **/
    @PostMapping("/getMenuList")
    public ResponseData<MenuResponseDTO> getMenuList() {
        log.info("getMenuList start ");
        ResponseData<MenuResponseDTO> responseData = new ResponseData<>();
        try {
            MenuResponseDTO responseDTOS = userServiceBz.getMenuTree();
            return responseData.success(responseDTOS);
        } catch (Exception e) {
            log.error("获取菜单列表失败");
            return responseData.fail("获取菜单列表失败");
        }
    }


    /**
     * @Author huchenying
     * @Description 添加菜单
     * @Date 2022/4/22 23:16
     **/
    @PostMapping("/addMenu")
    public ResponseData<MenuResponseDTO> addMenu(@Validated(Add.class) @RequestBody MenuRequestDTO requestDTO) {
        log.info("addMenu start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<MenuResponseDTO> responseDTOResponseData = new ResponseData<>();
        try {
            MenuResponseDTO responseDTO = userServiceBz.addMenu(requestDTO);
            return responseDTOResponseData.success(responseDTO);
        } catch (Exception e) {
            log.error("添加失败");
            return responseDTOResponseData.fail("添加失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 删除菜单
     * @Date 2022/4/22 23:16
     **/
    @PostMapping("/deleteMenu")
    public ResponseData<Object> deleteMenu(@Validated(Delete.class) @RequestBody MenuRequestDTO requestDTO) {
        log.info("deleteMenu start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            userServiceBz.deleteMenu(requestDTO);
            return ResponseData.success();
        } catch (Exception e) {
            log.error("添加失败");
            return ResponseData.fail0("添加失败");
        }
    }

    /**
     * @Author huchenying
     * @Description 更新角色的菜单
     * @Date 2022/4/23 18:07
     **/
    @PostMapping("/updateRoleMenu")
    public ResponseData<Object> updateRoleMenu(@Validated(Update.class) @RequestBody MenuRequestDTO requestDTO) {
        log.info("updateRoleMenu start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            userServiceBz.updateRoleMenu(requestDTO);
            return ResponseData.success();
        } catch (Exception e) {
            log.error("添加失败");
            return ResponseData.fail0("添加失败");
        }
    }
}
